﻿package
{
	import com.thaiflashdev.net.HTTPSender;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.system.System;
	import flash.text.TextFieldType;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author heart (heart@howdoflash.net)
	 */
	[SWF(width = "300", height = "300", frameRate = "10")]
	public class GGTranslateClient_HTTPSender extends Sprite 
	{
		private var input:TextField;
		private var output:TextField;
		private var submit:MyBtn 
		public function GGTranslateClient_HTTPSender():void 
		{
			System.useCodePage = true;
			
			createLabel();
			createInputOutput();
			createSubmitBtn();
		}
		private function createLabel():void {
			var head:TextField = new TextField();
			head.text = "google translate client EN to TH";
			addChild(head);
			head.selectable = false;
			head.autoSize = flash.text.TextFieldAutoSize.LEFT;
			
			var en:TextField = new TextField();
			var th:TextField = new TextField();
			en.text = "EN";
			th.text  = "TH";
			addChild(en);
			addChild(th);
			en.x = 15;
			th.x = 15;
			en.y = 20;
			th.y = 140;
			en.selectable = th.selectable = false;
		}
		private function createInputOutput():void {
			input = new TextField();
			input.type = TextFieldType.INPUT;
			input.border = true;
			addChild(input);
			input.y = 20;
			input.width = 200;
			input.height = 80;
			input.x = 50;
			input.multiline = true;
			
			output = new TextField();
			output.height = 80;
			output.width = 200;
			output.x = 55;
			output.y = 140;
			addChild(output);
			output.border = true;
			output.borderColor = 0x999999;
			output.selectable = true;
		}
		private function createSubmitBtn():void {
			submit = new MyBtn("Translate");
			submit.x = 120;
			submit.y = 110;
			addChild(submit);
			submit.addEventListener(MouseEvent.CLICK , translate);
		}
		private function translate(e:MouseEvent):void {
			submit.changeLabel("Wait..");
			var s:HTTPSender = new HTTPSender("http://translate.google.co.th/translate_t", "get");
			s.addEventListener(Event.COMPLETE , sended );
			
			var sndVar:Object = {
				langpair:"en|th",
				text:input.text
			}
			
			s.addVarObject(sndVar);
			s.send();
		}
		private function sended(e:Event):void {
			submit.changeLabel("Translate");
			output.text = findWord(e.target.data);
		}
		private function findWord(pageHtml:String):String {
			var myPattern:RegExp = /<div id=result_box dir="ltr">(.*?)<\/div>/;  
			var n:Array = pageHtml.match(myPattern);
			return n[1];
		}
	}
}

import flash.events.MouseEvent;
class MyBtn extends flash.display.Sprite {
	private var label:flash.text.TextField;
	
	public function MyBtn(lb:String) {
		label= new flash.text.TextField();
		label.text = lb;
		label.autoSize = flash.text.TextFieldAutoSize.LEFT;
		label.selectable = false;
		addChild(label);
		this.graphics.beginFill(0xFFCC00);
		this.graphics.drawRect(label.x, label.y, label.width, label.height);
		this.addEventListener(MouseEvent.MOUSE_OVER , over);
		this.addEventListener(MouseEvent.MOUSE_OUT , out);
	}
	private function out(e:MouseEvent):void {
		this.graphics.clear();
		this.graphics.beginFill(0xFFCC00);
		this.graphics.drawRect(label.x, label.y, label.width, label.height);
	}
	private function over(e:MouseEvent):void {
		this.graphics.clear();
		this.graphics.beginFill(0xFF99cc);
		this.graphics.drawRect(label.x, label.y, label.width, label.height);
	}
	public function changeLabel(lb:String):void {
		label.text = lb;
		this.graphics.beginFill(0xFFCC00);
		this.graphics.drawRect(label.x, label.y, label.width, label.height);
		this.addEventListener(MouseEvent.MOUSE_OVER , over);
		this.addEventListener(MouseEvent.MOUSE_OUT , out);
	}
}